<?php echo $error; ?>
<div class="flexigrid">
    <div class="mDiv">
        <div class="ftitle">
            &nbsp;
        </div>
        <div title="Minimize/Maximize" class="ptogtitle">
            <span></span>
        </div>
    </div>
    <div id="main-table-box">
        <div class="tDiv tDiv4">
            <div class="row">
                <div class="large-12 columns centered">
                    <?php echo validation_errors('<p class="alert label">'); ?>
                    <form action="editarVehiculo" method="post" class="custom" enctype="multipart/form-data">
                        <input name="id" type="hidden" value="<?php echo $id; ?>"/>
                        <table class="responsive">
                            <tbody>
                                <tr>
                                    <td>Carroceria:</td>
                                    <td>
                                        <?php
                                        if (isset($_GET['c'])) {
                                            $carrocerias = $_GET['c'];
                                        } else {
                                            $carrocerias = $car[0]->carroceria_id;
                                        }
                                        ?>
                                        <select id="carroceria" name="carroceria_id">
                                            <?php
                                            foreach ($carroceria as $row) {
                                                if ($carrocerias == $row->id) {
                                                    echo '<option value="' . $row->id . '" selected>' . $row->tipo . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->id . '">' . $row->tipo . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>Tipo:</td>
                                    <td>
                                        <select name="tipo">
                                            <?php
                                            foreach ($tipo as $row) {
                                                if ($car[0]->tipo == $row->tipo) {
                                                    echo '<option value="' . $row->tipo . '" selected>' . $row->tipo . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->tipo . '">' . $row->tipo . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Marca:</td>
                                    <td>
                                        <select name="marca">
                                            <?php
                                            foreach ($marcas as $row) {
                                                if ($car[0]->marca == $row->marca) {
                                                    echo '<option value="' . $row->marca . '" selected>' . $row->marca . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->marca . '">' . $row->marca . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>Modelo:</td>
                                    <td><input type="text" id="modelo" name="modelo" value="<?php echo $car[0]->modelo; ?>"></td>
                                </tr>
                                <tr>
                                    <td>Transmision:</td>
                                    <td>
                                        <select name="transmision">
                                            <?php
                                            foreach ($transmision as $row) {
                                                if ($car[0]->transmision == $row->tipo) {
                                                    echo '<option value="' . $row->tipo . '" selected>' . $row->tipo . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->tipo . '">' . $row->tipo . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>Categoria:</td>
                                    <td>
                                        <select name="categoria">
                                            <?php
                                            foreach ($categorias as $row) {
                                                if ($car[0]->categoria == $row->categoria) {
                                                    echo '<option value="' . $row->categoria . '" selected>' . $row->categoria . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->categoria . '">' . $row->categoria . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Combustible:</td>
                                    <td>
                                        <select name="combustible">
                                            <?php
                                            foreach ($combustibles as $row) {
                                                if ($car[0]->combustible == $row->combustible) {
                                                    echo '<option value="' . $row->combustible . '" selected>' . $row->combustible . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->combustible . '">' . $row->combustible . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>Año:</td>
                                    <td>
                                        <select name="anno">
                                            <?php
                                            for ($i = 2013; $i >= 1980; $i--) {
                                                if ($car[0]->anno == $i) {
                                                    echo '<option value="' . $i . '" selected>' . $i . '</option>';
                                                } else {
                                                    echo '<option value="' . $i . '">' . $i . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Precio:</td>
                                    <td>
                                        <select name="precio">
                                            <?php
                                            foreach ($precios as $row) {
                                                if ($car[0]->precio == $row->precio) {
                                                    echo '<option value="' . $row->precio . '" selected>$' . $row->precio . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->precio . '">$' . $row->precio . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <td>Cilindrada:</td>
                                    <td>
                                        <select name="cilindrada">
                                            <?php
                                            foreach ($cilindrada as $row) {
                                                if ($car[0]->cilindrada == $row->cilindrada) {
                                                    echo '<option value="' . $row->cilindrada . '" selected>' . $row->cilindrada . 'cc</option>';
                                                } else {
                                                    echo '<option value="' . $row->cilindrada . '">' . $row->cilindrada . 'cc</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Kilometraje:</td>
                                    <td><input type="text" name="kilometraje" value="<?php echo $car[0]->kilometraje; ?>"></td>
                                    <td>Color Exterior:</td>
                                    <td>
                                        <select name="color_exterior">
                                            <?php
                                            foreach ($colores as $row) {
                                                if ($car[0]->color_exterior == $row->color) {
                                                    echo '<option value="' . $row->color . '" selected>' . $row->color . '</option>';
                                                } else {
                                                    echo '<option value="' . $row->color . '">' . $row->color . '</option>';
                                                }
                                            }
                                            ?>
                                        </select>
                                    </td>
                                </tr>
                                <?php if ($carrocerias != 2 && $carrocerias != 3): ?>
                                    <tr class="vehiculo">
                                        <td>Color Interior:</td>
                                        <td>
                                            <select name="color_interior">
                                                <?php
                                                foreach ($colores as $row) {
                                                    if ($car[0]->color_interior == $row->color) {
                                                        echo '<option value="' . $row->color . '" selected>' . $row->color . '</option>';
                                                    } else {
                                                        echo '<option value="' . $row->color . '">' . $row->color . '</option>';
                                                    }
                                                }
                                                ?>
                                            </select>
                                        </td>
                                        <td>Puertas:</td>
                                        <td>
                                            <select name="puertas">
                                                <?php
                                                for ($i = 2; $i < 5; $i++) {
                                                    if ($car[0]->puertas == $i) {
                                                        echo '<option value="' . $i . '" selected>' . $i . '</option>';
                                                    } else {
                                                        echo '<option value="' . $i . '">' . $i . '</option>';
                                                    }
                                                }
                                                ?>
                                            </select>
                                        </td>
                                    </tr>

                                    <tr class="vehiculo">
                                        <td colspan="4">
                                            Extras:
                                            <div class="row">
                                                <div class="large-12 columns">
                                                    <label><input type="checkbox" class="check small button secondary radius" /> Marcar todas</label>
                                                </div>
                                                <div id="extras">
                                                    <?php foreach ($extras as $row) : ?>
                                                        <div class="large-3 columns">
                                                            <label>
                                                                <?php $input_name = $row->input_name; ?>
                                                                <?php if ($car[0]->$input_name == '0'): ?>
                                                                    <input type="checkbox" name="<?php echo $row->input_name; ?>" class="extras" checked="checked">
                                                                <?php else: ?>
                                                                    <input type="checkbox" name="<?php echo $row->input_name; ?>" class="extras">
                                                                <?php endif; ?>
                                                                <?php echo $row->extra; ?></label>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                                <tr>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;1: <?php echo $car[0]->imagen1; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen1; ?>"/>
                                        <input type="file" name="imagen1">
                                    </td>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;6: <?php echo $car[0]->imagen6; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen6; ?>"/>
                                        <input type="file" name="imagen6">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;2: <?php echo $car[0]->imagen2; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen2; ?>"/>
                                        <input type="file" name="imagen2">
                                    </td>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;7: <?php echo $car[0]->imagen7; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen7; ?>"/>
                                        <input type="file" name="imagen7">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2"><strong>Imagen N&ordm;3: <?php echo $car[0]->imagen3; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen3; ?>"/>
                                        <input type="file" name="imagen3">
                                    </td>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;8: <?php echo $car[0]->imagen8; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen8; ?>"/>
                                        <input type="file" name="imagen8">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2"><strong>Imagen N&ordm;4: <?php echo $car[0]->imagen4; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen4; ?>"/>
                                        <input type="file" name="imagen4">
                                    </td>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;9: <?php echo $car[0]->imagen9; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen9; ?>"/>
                                        <input type="file" name="imagen9">
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2"><strong>Imagen N&ordm;5: <?php echo $car[0]->imagen5; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen5; ?>"/>
                                        <input type="file" name="imagen5">
                                    </td>
                                    <td colspan="2">
                                        <strong>Imagen N&ordm;10: <?php echo $car[0]->imagen10; ?></strong><br/>
                                        <img src="<?php echo base_url() . 'cars/thumbs/' . $car[0]->imagen10; ?>"/>
                                        <input type="file" name="imagen10">
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <label>
                                            <?php if ($car[0]->nuevo == 0): ?>
                                                <input type="checkbox" name="nuevo" checked="checked">
                                            <?php else: ?>
                                                <input type="checkbox" name="nuevo">
                                            <?php endif; ?>
                                            Aparecer como nuevo
                                        </label>
                                    </td>
                                    <td>
                                        <label>
                                            <?php if ($car[0]->oferta == 0): ?>
                                                <input type="checkbox" name="oferta" checked="checked">
                                            <?php else: ?>
                                                <input type="checkbox" name="oferta">
                                            <?php endif; ?>
                                            Aparecer en oferta
                                        </label>
                                    </td>
                                    <td colspan="2">Observaciones: <textarea name="observaciones"><?php echo $car[0]->observaciones; ?></textarea></td>
                                </tr>
                                <tr>
                                    <td colspan="4"><input type="submit" id="submit" class="secondary-nav small radius button" value="Guardar"></td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('.check').click(function() {
            var $checkboxes = $(this).parents('.vehiculo').find('input[type=checkbox]');
            $checkboxes.prop('checked', $(this).is(':checked') ? true : false);
        });
        $('#carroceria').change(function() {
            var id = <?php echo $car[0]->id; ?>;
            window.location = 'editarVehiculo?id=' + id + '&c=' + $(this).val();
        });
        $('form').submit(function() {
            if ($('#modelo').val() == '') {
                alert('El campo del modelo es requerido!');
                $('#modelo').focus();
                return false;
            }
        });
    })
</script>